//// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
//package com.linqingying.gitcode.pullrequest.ui.toolwindow
//
//import com.intellij.collaboration.ui.toolwindow.dontHideOnEmptyContent
//import com.intellij.collaboration.ui.toolwindow.manageReviewToolwindowTabs
//import com.intellij.openapi.actionSystem.CommonShortcuts
//import com.intellij.openapi.actionSystem.DefaultActionGroup
//import com.intellij.openapi.actionSystem.ex.ActionUtil
//import com.intellij.openapi.application.EDT
//import com.intellij.openapi.components.Service
//import com.intellij.openapi.components.service
//import com.intellij.openapi.components.serviceAsync
//import com.intellij.openapi.project.DumbAware
//import com.intellij.openapi.project.Project
//import com.intellij.openapi.wm.ToolWindow
//import com.intellij.openapi.wm.ToolWindowFactory
//import com.intellij.openapi.wm.ToolWindowManager
//import com.intellij.openapi.wm.impl.content.ToolWindowContentUi
//import com.intellij.platform.util.coroutines.childScope
//import com.intellij.ui.IconManager
//import com.intellij.ui.JBColor
//import com.intellij.util.cancelOnDispose
//import com.intellij.util.concurrency.annotations.RequiresEdt
//import com.intellij.util.ui.JBUI
//import kotlinx.coroutines.*
//import kotlinx.coroutines.flow.*
//
//import com.linqingying.gitcode.i18n.GitCodeBundle
//import com.linqingying.gitcode.icons.GitCodeIcons
//import com.linqingying.gitcode.pullrequest.action.GCPRActionKeys
//import com.linqingying.gitcode.pullrequest.action.GCPRSelectPullRequestForFileAction
//import com.linqingying.gitcode.pullrequest.action.GCPRSwitchRemoteAction
//import com.linqingying.gitcode.pullrequest.ui.toolwindow.model.GCPRToolWindowViewModel
//import javax.swing.UIManager
//
//internal class GCPRToolWindowFactory : ToolWindowFactory, DumbAware {
//  override fun init(toolWindow: ToolWindow) {
//    toolWindow.setStripeShortTitleProvider(GitCodeBundle.messagePointer("toolwindow.stripe.Pull_Requests.shortName"))
//  }
//
//  override suspend fun manage(toolWindow: ToolWindow, toolWindowManager: ToolWindowManager) {
//    toolWindow.project.serviceAsync<GCPRToolWindowController>().manageIconInToolbar(toolWindow)
//  }
//
//  override fun createToolWindowContent(project: Project, toolWindow: ToolWindow) =
//    project.service<GCPRToolWindowController>().manageContent(toolWindow)
//
//  override fun shouldBeAvailable(project: Project): Boolean = false
//}
//
//@Service(Service.Level.PROJECT)
//private class GCPRToolWindowController(private val project: Project, parentCs: CoroutineScope) {
//  private val cs = parentCs.childScope(Dispatchers.Main)
//
//  @OptIn(ExperimentalCoroutinesApi::class)
//  suspend fun manageIconInToolbar(toolWindow: ToolWindow) {
//    coroutineScope {
//      val vm = project.serviceAsync<GCPRToolWindowViewModel>()
//      launch {
//        vm.isAvailable.collect {
//          withContext(Dispatchers.EDT) {
//            toolWindow.isAvailable = it
//          }
//        }
//      }
//
//      launch {
//        vm.activationRequests.collect {
//          withContext(Dispatchers.EDT) {
//            toolWindow.activate(null)
//          }
//        }
//      }
//
//      val focusColor = UIManager.getColor("ToolWindow.Button.selectedForeground")
//      launch {
//        vm.projectVm
//          .filterNotNull().flatMapLatest { it.listVm.hasUpdates }
//          .distinctUntilChanged()
//          .collectLatest {
//            withContext(Dispatchers.EDT) {
//              toolWindow.setIcon(
//                if (!it) GitCodeIcons.PullRequestsToolWindow
//                else IconManager.getInstance().withIconBadge(GitCodeIcons.PullRequestsToolWindow, JBColor {
//                  if (toolWindow.isActive) focusColor else JBUI.CurrentTheme.IconBadge.INFORMATION
//                }))
//            }
//          }
//      }
//    }
//  }
//
//  @RequiresEdt
//  fun manageContent(toolWindow: ToolWindow) {
//    toolWindow.component.putClientProperty(ToolWindowContentUi.HIDE_ID_LABEL, "true")
//
//    cs.launch {
//      val vm = project.serviceAsync<GCPRToolWindowViewModel>()
//
//      coroutineScope {
//        toolWindow.contentManager.addDataProvider {
//          if (GCPRActionKeys.PULL_REQUESTS_PROJECT_VM.`is`(it)) vm.projectVm.value
//          else null
//        }
//
//        // so it's not closed when all content is removed
//        toolWindow.dontHideOnEmptyContent()
//        val componentFactory = GCPRToolWindowTabComponentFactory(project, vm)
//        manageReviewToolwindowTabs(this, toolWindow, vm, componentFactory, GitCodeBundle.message("toolwindow.stripe.Pull_Requests"))
//        val wrapper = ActionUtil.wrap("GitCode.Create.Pull.Request")
//        wrapper.registerCustomShortcutSet(CommonShortcuts.getNew(), toolWindow.component)
//        toolWindow.setTitleActions(listOf(wrapper, GCPRSelectPullRequestForFileAction()))
//        toolWindow.setAdditionalGearActions(DefaultActionGroup(GCPRSwitchRemoteAction()))
//
//        awaitCancellation()
//      }
//    }.cancelOnDispose(toolWindow.contentManager)
//  }
//}
